using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ORSLibrary.Service;
using ORSLibrary.Common;
using System.Web;

namespace ORSLibrary.DAL
{
	public partial class CompanyUser : BusinessEntity
	{
        #region Public Properties
		
        public int ID { get; set; }
		public int CompanyID { get; set; }
		public string Name { get; set; }
		public string Username { get; set; }
		public string Password { get; set; }
		public int CreatedBy { get; set; }
		public DateTime CreatedDate { get; set; }
		public int ModifiedBy { get; set; }
		public DateTime ModifiedDate { get; set; }
		public bool IsDeleted { get; set; }
		
        #endregion
        
		public CompanyUser()
		{
            IsNew = true;
		}
		
		public CompanyUser(int ID)
		{
			SqlService sql = new SqlService();
			sql.AddParameter("@ID", SqlDbType.Int, ID);
			SqlDataReader reader = sql.ExecuteSPReader("CompanyUserSelect");
			
			if (reader.Read()) 
			{
				this.LoadFromReader(reader);
				reader.Close();
			}
			else
			{
				if (!reader.IsClosed) reader.Close();
				throw new ApplicationException("CompanyUser does not exist.");
			}
		}
		
		public CompanyUser(SqlDataReader reader)
		{
			this.LoadFromReader(reader);
		}
		
		protected void LoadFromReader(SqlDataReader reader)
		{
			if (reader != null && !reader.IsClosed)
			{
				ID = (int)reader["ID"];
                
				CompanyID = reader["CompanyID"] == DBNull.Value ? 0 : (int)reader["CompanyID"];
				Name = reader["Name"] == DBNull.Value ? string.Empty : (string)reader["Name"];
				Username = reader["Username"] == DBNull.Value ? string.Empty : (string)reader["Username"];
				Password = reader["Password"] == DBNull.Value ? string.Empty : (string)reader["Password"];
				CreatedBy = reader["CreatedBy"] == DBNull.Value ? 0 : (int)reader["CreatedBy"];
				CreatedDate = reader["CreatedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["CreatedDate"];
				ModifiedBy = reader["ModifiedBy"] == DBNull.Value ? 0 : (int)reader["ModifiedBy"];
				ModifiedDate = reader["ModifiedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["ModifiedDate"];
				IsDeleted = reader["IsDeleted"] == DBNull.Value ? false : (bool)reader["IsDeleted"];
                
                IsNew = false;
			}
		}
		
		public void Delete()
		{
			CompanyUser.Delete(ID);
		}
        
        public void Save()
		{
            if(IsNew)
                Create();
            else
                Update();
        }
		
		public void Update()
		{
            ModifiedBy = (int)HttpContext.Current.Session["UserID"];

            SqlService sql = new SqlService();

			sql.AddParameter("@ID", SqlDbType.Int, ID);			
            FillParamters(sql);
		
			sql.ExecuteSP("CompanyUserUpdate");
		}
		
		public void Create()
		{
            CreatedBy = (int)HttpContext.Current.Session["UserID"];
            ModifiedBy = (int)HttpContext.Current.Session["UserID"];
            
			SqlService sql = new SqlService();

            FillParamters(sql);
			
			sql.ExecuteSP("CompanyUserInsert");
            
            ID = (int)sql.Parameters["@ID"].Value;
		}
        
        public void FillParamters(SqlService sql)
		{
			sql.AddParameter("@CompanyID", SqlDbType.Int, CompanyID);
			sql.AddParameter("@Name", SqlDbType.NVarChar, Name);
			sql.AddParameter("@Username", SqlDbType.NVarChar, Username);
			sql.AddParameter("@Password", SqlDbType.NVarChar, Password);
			sql.AddParameter("@CreatedBy", SqlDbType.Int, CreatedBy);
			sql.AddParameter("@CreatedDate", SqlDbType.DateTime, CreatedDate);
			sql.AddParameter("@ModifiedBy", SqlDbType.Int, ModifiedBy);
			sql.AddParameter("@ModifiedDate", SqlDbType.DateTime, ModifiedDate);
			sql.AddParameter("@IsDeleted", SqlDbType.Bit, IsDeleted);
        }
		
		public static CompanyUser NewEntity(int ID)
		{
			CompanyUser newEntity = new CompanyUser();
			newEntity.ID = ID;

			return newEntity;
		}
		
        public static CompanyUser GetEntity(int ID)
		{
			return new CompanyUser(ID);
		}
		
		public static void Delete(int ID)
		{
			SqlService sql = new SqlService();
			sql.AddParameter("@ID", SqlDbType.Int, ID);
            sql.AddParameter("@ModifiedBy", SqlDbType.Int, HttpContext.Current.Session["UserID"]);
	
			sql.ExecuteSP("CompanyUserDelete");
		}
        
        public static CompanyUserList GetList(bool isComboBox)
		{
			CompanyUserList list = new CompanyUserList();
			
            SqlService sql = new SqlService();
			SqlDataReader reader = sql.ExecuteSPReader("CompanyUserSelectAll");
			
			while (reader.Read()) 
			{
                list.Add(new CompanyUser(reader));
			}
            reader.Close();
            
			return list;
		}
	}
    public partial class CompanyUserList : List<CompanyUser>
	{
    }
}
